<?php
require_once("dao/IDao.php");
require_once("model/Event.php");

class EventDAO{
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function getLatestEvent(){
		$event = null;
		try{
			if($stmt = $this->conn->prepare("CALL GET_LATEST_EVENT_ID()")){
				$stmt->execute();
				$stmt->bind_result($_eventid,$_eventyear);
				while($stmt->fetch()){
					$event = new Event();
					$event->eventId = $_eventid;
					$event->year = $_eventyear;
				}
			}
			else{
				throw new Exception($stmt->error);
			}	
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
		
		return $event;
	}
	
	public function getItem($eventid){
		$event = null;
		try{
			if($stmt = $this->conn->prepare("CALL EVENT_READ_ITEM(?)")){
				$stmt->bind_param("i",$eventid);
				$stmt->execute();
				
				$stmt->bind_result($_eventid,$_eventyear);
				$event = new Event();
				while($stmt->fetch()){
					$event->eventId = $_eventid;
					$event->year = $_eventyear;
				}
			}
			else{
				throw new Exception($stmt->error);
			}	
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		
		$this->conn->close();
		return $event;
	}
	
}


?>